package com.ytclub.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.ytclub.auth.application.convent.AuthPermissionDTOConverter;
import com.ytclub.auth.application.dto.AuthPermissionDTO;
import com.ytclub.auth.domain.entity.AuthPermissionBO;
import com.ytclub.auth.domain.service.AuthPermissionDoaminService;
import com.ytclub.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限控制层类
 *
 * @author: HuangQi
 * @date: Created in 14:56 2024/3/24
 * @description: 权限控制层
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {


    @Resource
    private AuthPermissionDoaminService authPermissionDoaminService;

    /**
     * 新增权限
     *
     * @param authPermissionDTO 新增权限的信息
     * @return 新增结果
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            // 参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名称不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()), "权限唯一标识不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父id不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getType(), "权限类型不能为空");

            // 调用领域层
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean registerResult = authPermissionDoaminService.add(authPermissionBO);

            // 返回结果
            return Result.ok(registerResult);
        } catch (Exception e) {
            // 异常处理
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }


    /**
     * 更新权限
     *
     * @param authPermissionDTO 更新权限的信息
     * @return 更新结果
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");

            // 调用领域层
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean registerResult = authPermissionDoaminService.update(authPermissionBO);

            // 返回结果
            return Result.ok(registerResult);
        } catch (Exception e) {
            // 异常处理
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新权限失败");
        }
    }

    /**
     * 删除权限(逻辑删除)
     *
     * @param authPermissionDTO 更新权限的信息
     * @return 更新结果
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");

            // 调用领域层
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean registerResult = authPermissionDoaminService.delete(authPermissionBO);

            // 返回结果
            return Result.ok(registerResult);
        } catch (Exception e) {
            // 异常处理
            log.error("PermissionController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除权限失败");
        }
    }

    /**
     * 查询用户权限
     *
     * @param authPermissionDTO 更新权限的信息
     * @return 更新结果
     */
    @RequestMapping("getPermission")
    public Result<String> getPermission(@RequestParam String userName) {
        try {
            log.info("PermissionController.getPermission.userName:{}", userName);
            // 参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(userName), "用户id不能为空");

            // 调用领域层
            List<String> permissionList = authPermissionDoaminService.getPermission(userName);

            // 返回结果
            return Result.ok(permissionList);
        } catch (Exception e) {
            // 异常处理
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("查询用户权限失败");
        }
    }

}
